define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, Groupseckill_agent, $window, $timeout, $ionicSlideBoxDelegate, GoPage, GoDialog, $stateParams) {
		$scope.scope = new Object();
		$scope.act_id = $stateParams.act_id;
		var goods_rule = new Array();
		$scope.goods_rule = new Array();
		$scope.choose_store = new Array();
		$scope.is_all = false;
		var goods = new Array();
		var region = new Array();
		$scope.show_store = new Array();
		$scope.show_region = new Array();
		$scope.region = new Array();
		$scope.scope.choose = '';
		$scope.scope.choose_select = { region_id: 0, region_name: "请选择区域" };
		$scope.store = new Array();
		$scope.detail = new Object();
		$scope.detail.image = new Array();
		$scope.isLoad = false; //可以加载
		//画面加载
		$scope.$on('$ionicView.afterEnter', function () {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function (index) {
			//手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function (index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
		};
		$scope.doSelectRegion = function () {
			var list = new Array();
			$scope.dealer.region.forEach(obj => {
				if (obj.id == undefined)
					obj.id = obj.region_id;
				if (obj.name == undefined)
					obj.name = obj.region_name;
			});
			//id是值 name是显示
			GoDialog.showSelect("区域", $scope.dealer.region, $scope.scope.choose_select, function (data) {
				$scope.scope.choose = data.region_name;
				$scope.scope.choose_select = data;
			});
		}
		/*//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}, 1000);
		};*/
		$scope.get_region = function () {
			$scope.get_store();
			if ($scope.scope.choose) {
				if ($scope.choose_store.length > 0) {
					for (var i = 0; i < $scope.dealer.region.length; i++) {
						if ($scope.dealer.region[i].region_name == $scope.scope.choose) {
							for (var j = 0; j < $scope.region.length; j++) {
								if ($scope.dealer.region[i].region_id == $scope.region[j].region_id) {
									$scope.region[i] = {
										"region_id": $scope.dealer.region[i].region_id,
										"is_all": $scope.scope.is_all,
										"store": $scope.choose_store
									}
									$scope.show_region[i] = {
										"region_name": $scope.dealer.region[i].region_name,
										"store": $scope.show_store
									}
									$scope.choose_store = new Array();
									$scope.show_store = new Array();
									return;
								}

							}
							var n = $scope.region.length;
							$scope.region[n] = {
								"region_id": $scope.dealer.region[i].region_id,
								"is_all": $scope.scope.is_all,
								"store": $scope.choose_store
							};
							$scope.show_region[n] = {
								"region_name": $scope.dealer.region[i].region_name,
								"store": $scope.show_store
							}
							$scope.choose_store = new Array();
							$scope.show_store = new Array();

						}
					}
				} else
					GoDialog.toast("请选择代理商");
			} else
				GoDialog.toast("请选择地域");
			$scope.choose_store = new Array();
			region = $scope.region;
			console.log($scope.region);

		}
		$scope.get_store = function () {
			for (var i = 0; i < $scope.store.length; i++) {
				if ($scope.store[i].checked == true) {
					var n = $scope.choose_store.length;
					$scope.choose_store[n] = {
						"store_id": $scope.store[i].store_id
					};
					$scope.show_store[n] = {
						"store_name": $scope.store[i].store_name
					}
				}

			}
		}
		$scope.delete_store = function (region_name, index) {
			for (var i = 0; i < $scope.show_region.length; i++) {
				if ($scope.show_region[i].region_name == region_name) {
					region[i].store.splice(index, 1);
					$scope.show_region[i].store.splice(index, 1);
				}
			}

		}
		$scope.delete_region = function (index) {
			$scope.show_region.splice(index, 1);
			region.splice(index, 1);
		}
		$scope.select_all = function (all) {
			if (all == true) {
				$scope.scope.is_all = true;
				for (var i = 0; i < $scope.store.length; i++) {
					$scope.store[i].checked = true;
				}
			} else {
				$scope.scope.is_all = false;
				for (var i = 0; i < $scope.store.length; i++) {
					$scope.store[i].checked = false;
				}
			}

			console.log($scope.store);
		}
		$scope.checked = function (id) {
			console.log(id);
			for (var i = 0; i < $scope.store.length; i++) {
				if ($scope.store[i].store_id == id) {
					if ($scope.store[i].checked == false) {
						$scope.scope.is_all = false;

						break;
					} else {
						if ($scope.store.length == 1) {
							$scope.scope.is_all = true;

						}

					}
				} else if ($scope.store[i].checked == true) {
					$scope.scope.is_all = true;


				} else {

					$scope.scope.is_all = false;
					break;
				}
			}
			//			console.log()
			console.log($scope.dealer);
		}
		$scope.get_goods = function () {
			goods = new Array();
			console.log($scope.dealer);
			for (var i = 0; i < $scope.dealer.goods.length; i++) {
				if ($scope.dealer.goods[i].seckill_price != undefined && $scope.dealer.goods[i].seckill_price != '' && $scope.dealer.goods[i].prize_price != '') {
					var n = goods.length;
					goods[n] = {
						"goods_id": $scope.dealer.goods[i].goods_id,
						"seckill_price": $scope.dealer.goods[i].seckill_price,
						"prize_price": $scope.dealer.goods[i].prize_price
					};
				}
			}
			if (goods.length < 1) {
				GoDialog.toast("活动商品信息未完整");
			}
		}
		$scope.get_third = function () {
			//看情况取消注释
			$scope.get_region();


		}
		$scope.submit = function () {
			$scope.get_goods();
			if (region.length < 1) {
				GoDialog.toast("请确定您已经保存修改的促销区域信息");
				return;
			}
			for (var i = 0; i < region.length; i++) {
				if (region[i].store == undefined) {
					GoDialog.toast("请填写完整的代理商信息");
					return;
				} else if (region[i].store.length < 1) {
					GoDialog.toast("请填写完整的代理商信息");
					return;
				}
			}
			Groupseckill_agent.seckill_edit($scope.act_id, region, goods, function (data) {
				if (data.success == true) {
					GoDialog.toast("修改成功");
				} else
					GoDialog.toast("修改失败");
			})
		}
		//加载数据
		$scope.init_data = function () {
			Groupseckill_agent.seckill_detail($scope.act_id, function (data) {
				$scope.detail = data;
			});
			Groupseckill_agent.seckill_dealer($scope.act_id, function (data) {
				$scope.dealer = data;
				console.log(data);
				$scope.$watch('scope.choose', function (newValue, oldValue) {
					for (var i = 0; i < $scope.dealer.region.length; i++) {
						if ($scope.dealer.region[i].region_name == newValue) {
							$scope.store = $scope.dealer.region[i].store;
							console.log($scope.store);
							$scope.select_all($scope.dealer.region[i].is_all);

						}
					}
				})
			});
		}



	}

	controller.$inject = ['$scope', 'Groupseckill_agent', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage', 'GoDialog', '$stateParams'];

	return app.registerController("groupseckill_agentctrl", controller);
});